/**
 * 发送邮件的服务
 * 可以借助smtp.qq.com smtp.163.com等服务器发送验证邮件
 * node-mailer
 */
var nodeMailer = require('nodemailer');

/**
 * @param string username   用户密码
 * @param string _id    用户id
 * @param string email  用户email地址
 * @param function callback 回调函数, 用于接收错误信息
 */
function sendMail(_id,username,email,callback){
    var transporter = nodeMailer.createTransport({
        //定义用来发送的邮件服务器
        host: 'smtp.qq.com',
        auth:{
            //账户
            user: '2030013981@qq.com',
            //smtp服务器的授权码
            pass: 'xdgttfcjvbogcgfd'
        },
    });
    var mailContent = '<h3>Welcome to register our website! '+username+', please click <a href="http://localhost:3000/verifyAccount/' + _id + '">here </a> to activate your account!</h3>';
    mailContent += " <p>If the link above is unavaliable, please click http://localhost:3000/verifyAccount/"+ _id + " to activate your account."
    //发送邮件选项
    var mailOption = {
        from: 'Test Project Admin<2030013981@qq.com>',
        to: email,  //目标收件地址
        subject: 'Welcome to register test project!',   //邮件标题头
        html: mailContent
    };

    transporter.sendMail(mailOption,function (err,msg) {
        console.log("=== email send error: "+err);
        console.log("=== email send msg: "+msg);
        //向外抛出异常
        callback(err);
    });
}

module.exports = sendMail;




